package cn.mooncode.basketballManage.service.impl;

import cn.mooncode.basketballManage.pojo.Playerinfo;
import cn.mooncode.basketballManage.pojo.RespBean;
import cn.mooncode.basketballManage.pojo.RespPageBean;
import cn.mooncode.basketballManage.service.ITeamService;
import cn.mooncode.basketballManage.mapper.TeamMapper;
import cn.mooncode.basketballManage.pojo.Team;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jwz
 * @since 2021-03-28
 */
@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements ITeamService {

    @Autowired
    private TeamMapper teamMapper;

    /**
     * 添加教练信息
     * @param cid
     * @param tid
     * @return
     */
    @Override
    public RespBean addCoachInfo(Integer cid, Integer tid) {
        if (teamMapper.addCoachInfo(cid, tid)) {
            return RespBean.success("添加教练成功！");
        }
        return RespBean.error("添加教练失败！");
    }

    /**
     * 添加球员信息
     * @param playerinfoList
     * @param tid
     * @return
     */
    @Override
    public RespBean addPlayerInfo(List<Playerinfo> playerinfoList, Integer tid) {
        if (teamMapper.addPlayerInfo(playerinfoList, tid)) {
            return RespBean.success("添加球员成功！");
        }
        return RespBean.error("添加球员失败！");
    }

    /**
     * 获取所有球队信息
     * @param currentPage
     * @param size
     * @param team
     * @return
     */
    @Override
    public RespPageBean getAllTeams(Integer currentPage, Integer size, Team team) {
        Page<Team> page = new Page<>(currentPage, size);
        IPage<Team> teamIPage = teamMapper.getAllTeams(page, currentPage, size);
        return new RespPageBean(teamIPage.getTotal(), teamIPage.getRecords());
    }

    /**
     * 获取当前球队信息
     * @param uid
     * @param rid
     * @return
     */
    @Override
    public Team getCurrentTeam(Integer uid, Integer rid) {
        return teamMapper.getCurrentTeam(uid, rid);
    }

    /**
     * 查询本身以外的球队信息
     * @param tid
     * @return
     */
    @Override
    public RespBean getTeam(Integer tid) {
        List<Team> teamList = teamMapper.getTeam(tid);
        if (teamList != null) {
            return RespBean.success("查询成功！", teamList);
        }
        return RespBean.error("查询失败！");
    }

    /**
     * 修改球队获胜次数
     * @param tid
     * @return
     */
    @Override
    public RespBean updateWinNum(Integer tid) {
        if(teamMapper.updateWinNum(tid)) {
            return RespBean.success("修改球队获胜次数失败成功！");
        }
        return RespBean.error("修改球队获胜次数失败！");
    }
}
